
package games.mapacman.client;

import java.awt.Component;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import java.util.Hashtable;

import javax.swing.ImageIcon;

public class Images {

	private MediaTracker tracker;
	private Toolkit tk;
  private Hashtable<String, Image> images;

	public Images(Component parent) {
		tracker = new MediaTracker(parent);
    images = new Hashtable<String, Image>();
		tk = parent.getToolkit();
		loadImages();
	}


	private void loadImages() {
		String[] imageList = {"eatensign.png",
				"dot.png","superdot.png","fruits.png","powerpill.png",
				"GhostG.png","GhostR.png",
				"mplayer.png","oplayer.png","pplayer.png",
				"wall0.png","wall1.png","wall2.png","wall3.png","wall4.png","wall5.png","wall6.png","wall7.png","wall8.png","wall9.png","wall10.png","wall11.png","wall12.png","wall12.png","wall13.png","wall14.png","wall15.png"
				,"zonechange.png"}; 
		
		// TODO : don't like it hardcoded ... but there's no other way if it should work with JARs
		/*
		try {
			URI uri = MaPacman.class.getResource("games/mapacman/client/images/").toURI();
			imageList = new File(uri).list();
		} catch (URISyntaxException e1) {
			e1.printStackTrace();
		}

		Enumeration<URL> url=null;
		try {
			url = MaPacman.class.getClassLoader().getResources("games/mapacman/images/");
		} catch (IOException e) {
			e.printStackTrace();
		}

		for (; url.hasMoreElements() ;) {
	         System.out.println("a"+url.nextElement());

	     }*/
		
		
		for (int i = 0; i < imageList.length; i++) {
			String format = imageList[i].substring(imageList[i].length() - 3,imageList[i].length());
			if (format.equals("jpg") || format.equals("gif") || format.equals("png")) {
				String name = imageList[i].substring(0, imageList[i].length() - 4);
				//Image image = tk.getImage("games/mapacman/client/images/" + imageList[i]);
				Image image = new ImageIcon(MaPacman.class.getResource("/games/mapacman/client/images/" + imageList[i])).getImage();
				tracker.addImage(image, i);
				try {
					tracker.waitForID(i);
				}
				catch (InterruptedException e) {}
				images.put(name, image);
			}
		}
	}
	
	public Image getImage(String name) {
		return (Image) images.get(name);
	}
}
